package com.atguigu.day03;

import com.atguigu.bean.WaterSensor;
import com.atguigu.func.MyMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/7/10
 * 该案例演示了map算子
 * 需求：提取WaterSensor中的id字段的功能
 */
public class Flink09_trans_map {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 2.读取数据
        DataStreamSource<WaterSensor> ds = env.fromElements(
                new WaterSensor("sensor_1", 1L, 1),
                new WaterSensor("sensor_2", 2L, 2)
        );
        //TODO 3.从流中数据中提取传感器id
        /*//匿名内部类
        SingleOutputStreamOperator<String> mapDS = ds.map(
                new MapFunction<WaterSensor, String>() {
                    @Override
                    public String map(WaterSensor ws) throws Exception {
                        return ws.id;
                    }
                }
        );
        //lambda表达式
        SingleOutputStreamOperator<String> mapDS = ds.map(
                ws -> ws.id
        );
        //方法引用
        SingleOutputStreamOperator<String> mapDS = ds.map(WaterSensor::getId);
        */
        SingleOutputStreamOperator<String> mapDS = ds.map(new MyMapFunction());

        //TODO 4.打印输出
        mapDS.print();
        //TODO 5.提交作业
        env.execute();

    }
}